package com.runen.wnhz.runen.ui.adapter.puadapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.runen.wnhz.runen.R;
import com.runen.wnhz.runen.data.entity.LessonListEntity;
import com.runen.wnhz.runen.data.entity.TestListEntity;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018-04-26.
 */

public class TestAdapter extends RecyclerView.Adapter<TestAdapter.ViewHolder> {

    private MyItemClickListener mItemClickListener;
    private LayoutInflater inflater;
    private Context mContext;
    private TestListEntity mDatas;

    public TestAdapter(Context mContext, TestListEntity mDatas) {
        this.mContext = mContext;
        this.mDatas = mDatas;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_test, parent, false);
        return new ViewHolder(view, mItemClickListener);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        TestListEntity.ListBean dataEntity = holder.testEntity = mDatas.getList().get(position);
        // 测试名称
        holder.tvName.setText(dataEntity.getTest_name());
        // 测试题数
        holder.tvCount.setText("共计" + dataEntity.getTest_count() + "题");
        // 测试总分
        holder.tvScore.setText("总分：" + dataEntity.getTest_score() + "分");
        // is_test 1:测试过 2：未测试过
        if ("1".equals(dataEntity.getIs_test())) holder.tvTest.setText("再次测验");
        else holder.tvTest.setText("立即测验");
    }

    @Override
    public int getItemCount() {
        return mDatas.getList().size();
    }

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private MyItemClickListener mListener;
        TestListEntity.ListBean testEntity;

        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_count)
        TextView tvCount;
        @BindView(R.id.tv_score)
        TextView tvScore;
        @BindView(R.id.tv_test)
        TextView tvTest;

        public ViewHolder(View itemView, MyItemClickListener onItemClickListener) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.mListener = onItemClickListener;
            tvTest.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if (mListener != null) {
                mListener.onItemClick(view, testEntity);
            }
        }
    }

    /**
     * 创建一个回调接口
     */
    public interface MyItemClickListener {
        void onItemClick(View view, TestListEntity.ListBean testEntity);
    }

    /**
     * 在activity里面adapter就是调用的这个方法,将点击事件监听传递过来,并赋值给全局的监听
     * @param myItemClickListener
     */
    public void setItemClickListener(MyItemClickListener myItemClickListener) {
        this.mItemClickListener = myItemClickListener;
    }
}
